﻿/************************************************************************
 * SMLib - State Model Library
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published 
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details. 
 * ***********************************************************************
 * File         :     SMLib\Automata\MM\SUL_MM_Remote_DLL.cs
 * Description  :     To connect the learner into a wrapper in a DLL file
 * Change log   :     -Feb 2013:  Initial version
 * To Do        :     
 ************************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using SMLib.Automata.Base;
using SMLib.Automata.MM;

namespace SMLib.Automata.MM
{
    /// <summary>
    /// A system under learning (SUL) wrapper as a black box Mealy machine in remote using dynamic link library (DLL)
    /// <para>To use this class, put the dll file in the same directory and rename it into "mm.dll"</para>
    /// <remarks>It implementes IBlackBox_MealyMachine.
    /// The implementation is a DLL file which has the following entries:
    /// - void Reset()
    /// - string Step(string)
    /// </remarks>
    /// </summary>
    public class SUL_MM_Remote_DLL : IBlackBox_MealyMachine
    {
        class SUT_DLL
        {
            [DllImport("mm.dll", EntryPoint = "?Reset@@YAXXZ")]
            public static extern void Reset();

            [DllImport("mm.dll", CharSet = CharSet.Unicode, EntryPoint = "?Step@@YAPA_WPA_W@Z"
                , CallingConvention = CallingConvention.Cdecl)]
            public static extern string Step(string input);
        }

        #region IBlackBox_MealyMachine Members

        /// <summary>
        /// To reset the system. This method calls the reset function in DLL file
        /// </summary>
        public void Reset()
        {
            SUT_DLL.Reset();
        }

        /// <summary>
        /// Perform one step. This method calls the "Step" function in the dll
        /// <remarks>Note that it converts the input into string, calls the function in DLL by a string parameter and convert
        /// the string result into StringSymbol and returns it</remarks>
        /// </summary>
        /// <param name="input">the input symbol</param>
        /// <returns>the resultin output symbol</returns>
        public ISymbol Step(ISymbol input)
        {
            return new StringSymbol(SUT_DLL.Step(input.ToString()));
        }


        public Alphabet InputAlphabet
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
